package cn.e3mall.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.e3mall.common.utils.FileUtils;
import cn.e3mall.common.utils.JsonUtils;

/**
 * 图片上传处理Controller
 * <p>Title: PictureController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class PictureController {
	
	@RequestMapping(value="/pic/upload", produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")
	@ResponseBody
	public String uploadFile(MultipartFile uploadFile) {
		try {
			//取文件扩展名
			String originalFilename = uploadFile.getOriginalFilename();
			//封装到map中返回
			FileUtils fileUtils =new FileUtils();
			String urlString=fileUtils.upload(uploadFile,originalFilename);
			Map<Object, Object> result = new HashMap<Object, Object>();
			result.put("error", 0);
			result.put("url", urlString);
			return JsonUtils.objectToJson(result);
		} catch (Exception e) {
			e.printStackTrace();
			Map<Object, Object> result = new HashMap<Object, Object>();
			result.put("error", 1);
			result.put("message", "图片上传失败");
			return JsonUtils.objectToJson(result);
		}
	}
	
	
}
